package v3.object.command.servoCommand;

import java.util.ArrayList;
import java.util.Map;

import text.TextUtil;
import v3.object.STcfg;
import v3.object.command.aCommand;
import v3.object.command.iCommand;
import v3.object.ship.Allert;
import v3.object.ship.Ship;
import v3.object.space.Enemy;
import v3.object.space.Universe;

public class OnScreen extends servoCommand
{

	private String msg;

	public OnScreen(String commandName, String msg)
	{
		this.commandName = commandName;
		this.msg = msg;
	}

	private String separator = "";
	private String spacer = "                                                                                       ";

	// private String
	// spacer="*****************************************************************************************";

	@Override
	protected boolean realExecute(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		ArrayList<String> msgArray = new ArrayList<String>();
		msgArray.add(commandName);
		String parts[] = msg.split("\n");
		int maxLengh = 0;
		for (String s : parts)
		{
			msgArray.add(s);
			maxLengh = Math.max(maxLengh, s.length());
		}

		ArrayList<String> status = new ArrayList<String>();
		ArrayList<String> sideInfo = new ArrayList<String>();

		status.add("");
		status.add("* SHIP STATUS *");
		status.add("");
		status.add("STATUS:   -" + ship.allert + "-");
		status.add("POSITION: " + ship.getPosition());
		status.add("SECTOR:   " + ship.getSectorPosition());
		status.add("STARDATE: " + format_00.format(universe.starDate));
		status.add("ENERGY:   " + format_00.format(ship.energy));
		status.add("TORPEDO:  " + format_00.format(ship.thorpedo));
		status.add("SHILED:   " + (ship.isShieldON() ? "*ON*  - " : "OFF - ") + format_00.format(ship.shield));
		status.add("HULL:     " + format_00.format(ship.hull));
		status.add("SCORE:    " + format_00.format(ship.score));

		sideInfo.add("");
		sideInfo.add("");
		sideInfo.add("");
		sideInfo.add(" Legend: [Stars Enemy Bases]");
		sideInfo.add("");
		sideInfo.add(" Cursor:");
		sideInfo.add(" 8   1   2");
		sideInfo.add("   \\ | /");
		sideInfo.add(" 7 - * - 3");
		sideInfo.add("   / | \\");
		sideInfo.add(" 6   5   4");

		int statusMaxLengh = 0;
		for (String s : status)
			statusMaxLengh = Math.max(statusMaxLengh, s.length());

		for (int i = 0; i < status.size(); i++)
		{
			String s = status.get(i);
			String sideInfos = "";
			if (i < sideInfo.size())
				sideInfos = sideInfo.get(i);
			s = s + spacer.substring(0, statusMaxLengh - s.length() + 3) + "| " + sideInfos;
			status.set(i, s);
		}

		ArrayList<String> enemyStatus = new ArrayList<String>();
		if (ship.allert.equals(Allert.RED))
		{
			enemyStatus.add("");
			enemyStatus.add("- ENEMY STATUS -");
			int j = 0;
			for (long id : ship.getEnemyList().keySet())
			{
				Enemy e = ship.getEnemyList().get(id);
				j++;
				enemyStatus.add(TextUtil.append(j, " -", e.getInfo()));
			}
		}

		int lines = Math.max(msgArray.size(), status.size());

		ArrayList<String> onScreen = new ArrayList<String>();
		onScreen.add(separator);
		for (int i = 0; i < lines; i++)
		{
			String m = "";
			String stat = "";
			String enemy = "";
			String c = "";

			if (i < msgArray.size())
				m = msgArray.get(i);

			if (i < status.size())
				stat = status.get(i);

			if (i < enemyStatus.size())
				enemy = enemyStatus.get(i);

			if (!ship.allert.equals(Allert.RED))
				onScreen.add(TextUtil.append(fillBlankSpace(m, maxLengh), stat));
			else
				onScreen.add(TextUtil.append(fillBlankSpace(m, maxLengh), fillBlankSpace(stat, 20), enemy));

		}
		onScreen.add(separator);

		for (String line : onScreen)
			System.out.println(line);

		return true;
	}

	private String fillBlankSpace(String m, int maxLengh)
	{
		String res = m;

		for (int i = m.length(); i < maxLengh + 5; i++)
		{
			res = TextUtil.append(res, " ");
		}
		return res;
	}

}
